﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
namespace MVCManage.Api.Controllers
{
    public class RoleController : ApiController
    {
        // GET: api/Role
        RoleBLL role = new RoleBLL();
        public ResultData<List<RoleDto>> Get()
        {
            var zhi = role.RoleList();
            ResultData<List<RoleDto>> result = new ResultData<List<RoleDto>>();
            var kk = from n in zhi
                     select new RoleDto()
                     {
                         Rid = n.roleID,
                         Rname = n.roleName,
                         DeptID = n.Department.deptID,
                         DeptName = n.Department.deptName,
                         Remark = n.remark,
                         LastTime = n.lastTime,
                         Type = n.type
                     };
            result.data = kk.ToList();
            return result;

        }
        [Route("api/Roles/Page")]

        public ResultPageList<RoleDto> GetRolesPage(string name, int page, int size)
        {
            int count = 0;
            var zhi = role.GetRolesPagelist(name, page, size, out count);
            var list = from n in zhi
                       select new RoleDto()
                       {
                           Rid = n.roleID,
                           Rname = n.roleName,
                           DeptID = n.Department.deptID,
                           DeptName = n.Department.deptName,
                           Remark = n.remark,
                           LastTime = n.lastTime,
                           Type = n.type

                       };
            ResultPageList<RoleDto> result = new ResultPageList<RoleDto>();
            result.code = 1;
            result.msg = "ok";
            result.count = count;
            result.data = list.ToList();
            return result;


        }

        public Result PostRoles([FromBody] RoleDto value) {
          MVCManage.Model. Roles zhi =new MVCManage.Model.Roles();
            zhi.roleName = value.Rname;
            zhi.deptID=value.DeptID;
            zhi.type= value.Type;
            zhi.remark=value.Remark;
           zhi.lastTime=DateTime.Now;
            var pd = role.Add(zhi);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;

        }
        public Result PutRoles([FromBody] RoleDto value) {
            MVCManage.Model.Roles zhi = new MVCManage.Model.Roles();
            zhi.roleID = value.Rid;
            zhi.roleName = value.Rname;
            zhi.deptID = value.DeptID;
            zhi.type = value.Type;
            zhi.remark = value.Remark;
            zhi.lastTime = DateTime.Now;
            var pd = role.Update(zhi);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }

        public Result Delete(int id) {
            var pd = role.Delete(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }
        [Route("api/Roles/Deletes")]
        public Result Deletes(int[] ids)
        {
            var pd = role.Deletes(ids);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "ok" : "error";
            return result;
        }
    }
}
